using UnityEngine;

public class TargetPoint : MonoBehaviour {

    public Enemy Enemy { get; private set; }

    public Vector3 Position => transform.position;
    
    const int enemyLayerMask = 1 << 9;

    static Collider[] buffer = new Collider[100];

    public static int BufferedCount { get; private set; }

    public static bool FillBuffer (Vector3 position, float range) {
        Vector3 top = position;
        top.y += 3f;
        BufferedCount = Physics.OverlapCapsuleNonAlloc(
            position, top, range, buffer, enemyLayerMask
        );
        return BufferedCount > 0;
    }
    
    public static TargetPoint RandomBuffered =>
        GetBuffered(Random.Range(0, BufferedCount));

    public static TargetPoint GetBuffered (int index) {
        var target = buffer[index].GetComponent<TargetPoint>();
        Debug.Assert(target != null, "以非敌方为目标!", buffer[0]);
        return target;
    }
    
    void Awake () {
        Enemy = transform.root.GetComponent<Enemy>();
        Debug.Assert(Enemy != null, "没有 Enemy root 的目标点!", this);
        Debug.Assert(
            GetComponent<SphereCollider>() != null,
            "没有球体碰撞器的目标点!", this
        );
        Debug.Assert(gameObject.layer == 9, "错误图层上的目标点!", this);
    }
}